package com.i2works.smartluolong.web.entity.utils;

import com.google.common.collect.Maps;
import com.i2works.smartluolong.utils.base.RegExConstant;
import com.i2works.smartluolong.web.entity.user.UserLoginVO;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author koal <koal@vip.qq.com>
 * @date 2017/5/18 17:58
 */
public class ValidatorUtil {

    //错误消息存储
    private static Map<String, String> msgMap = null;
    //密码验证结果，默认成功
    private static Boolean passwordVal = Boolean.TRUE;
    //二次密码验证结果，默认成功
    private static Boolean confirmPasswordVal = Boolean.TRUE;
    //为空模板
    private final static String FORMAT_NULL = "%s不能为空。";
    //格式错误模板
    private final static String FORMAT_REGEX_ERROR = "%s格式不正确。";


    /**
     * 登录验证：http://www.domain.com/user/login
     */
    public static Map<String, String> login(UserLoginVO user) {
        Map<String, String> msgMap = Maps.newLinkedHashMap();
        //账号
        userNameAndEmail(msgMap, user.getUserName());
        //密码
        password(msgMap, user.getPassword());
        return msgMap;
    }

    /**
     * 注册验证：http://www.domain.com/user/register
     */
    public static Map<String, String> register(UserLoginVO user) {
        msgMap = Maps.newLinkedHashMap();
        //账号
        userName(msgMap, user.getUserName());
        //手机
        phone(msgMap, user.getPhone());
        //邮箱
        //email(msgMap, user.getEmail());
        //密码
        password(msgMap, user.getPassword());
        //二次密码
        confirmPassword(msgMap, user.getConfirmPassword());
        //2次密码是否相等
        passwordEquals(msgMap, user.getPassword(), user.getConfirmPassword());
        return msgMap;
    }

    /**
     * 找回密码 - 提交找回申请页面：http://www.domain.com/user/password
     */
    public static Map<String, String> password(UserLoginVO user) {
        msgMap = Maps.newLinkedHashMap();
        //账号
        userNameAndEmail(msgMap, user.getUserName());
        return msgMap;
    }

    /**
     * 找回密码 - 新密码提交页面：http://www.domain.com/user/resetPassword
     */
    public static Map<String, String> restPassword(UserLoginVO user) {
        msgMap = Maps.newLinkedHashMap();
        //密码
        password(msgMap, user.getPassword());
        //二次密码
        confirmPassword(msgMap, user.getConfirmPassword());
        //2次密码是否相等
        passwordEquals(msgMap, user.getPassword(), user.getConfirmPassword());
        return msgMap;
    }


    /**
     * 后台修改资料：http://www.domain.com/user/update
     */
    public static Map<String, String> update(UserLoginVO user) {
        msgMap = Maps.newLinkedHashMap();
        //邮箱
        email(msgMap, user.getEmail());

        //密码
        if (StringUtils.isNotBlank(user.getPassword())) {
            password(msgMap, user.getPassword());
        }

        //二次密码
        if (StringUtils.isNotBlank(user.getConfirmPassword())) {
            confirmPassword(msgMap, user.getConfirmPassword());
        }

        //2次密码是否相等(不为空，且相等)
        if (StringUtils.isNoneBlank(user.getPassCode(), user.getConfirmPassword()) && !StringUtils.equals(user.getPassCode(),user.getConfirmPassword())) {
            msgMap.put("confirmPasswordMsg", "两次输入的密码不相同。");
        }
        return msgMap;
    }


    /**
     * @param regex 正则表达式字符串
     * @param str   要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean matcher(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 验证字符串
     *
     * @param str         字符串
     * @param regex       正则
     * @param formatStr   错误提示占坑字符
     * @param formatRegex 正则错误占坑字符,为空占坑字符是formatStr
     * @return str        返回null为验证通过
     */
    private static String valStr(String str, String regex, String formatStr, String formatRegex) {
        if (StringUtils.isNotBlank(str)) {
            if (!matcher(regex, str)) {
                return String.format(FORMAT_REGEX_ERROR, StringUtils.isNotBlank(formatRegex) ? formatRegex : formatStr);
            }
        } else {
            return String.format(FORMAT_NULL, formatStr);
        }
        return null;
    }


    //账号和邮箱
    private static void userNameAndEmail(Map<String, String> msgMap, String userName) {
        String userNameMsg = valStr(userName, RegExConstant.USERNAME_EMAIL, "账号或邮箱", null);
//        String userNameMsg = valStr(userName, RegExConstant.USERNAME, "账号或邮箱", null);
//        String emailMsg = valStr(userName, RegExConstant.email, "账号或邮箱", null);
        if (StringUtils.isNotBlank(userNameMsg)) {
            msgMap.put("userNameMsg", userNameMsg);
        }
    }

    //账号
    private static void userName(Map<String, String> msgMap, String userName) {
        String userNameMsg = valStr(userName, RegExConstant.USERNAME, "账号", null);
        if (StringUtils.isNotBlank(userNameMsg)) {
            msgMap.put("userNameMsg", userNameMsg);
        }
    }

    //手机
    private static void phone(Map<String, String> msgMap, String phone) {
        String phoneMsg = valStr(phone, RegExConstant.PHONE, "手机号", null);
        if (StringUtils.isNotBlank(phoneMsg)) {
            msgMap.put("phoneMsg", phoneMsg);
        }
    }


    //邮箱
    private static void email(Map<String, String> msgMap, String email) {
        String emailMsg = valStr(email, RegExConstant.EMAIL, "邮箱", null);
        if (StringUtils.isNotBlank(emailMsg)) {
            msgMap.put("emailMsg", emailMsg);
        }
    }

    //密码
    private static void password(Map<String, String> msgMap, String password) {
        String passwordMsg = valStr(password, RegExConstant.PASSWORD, "密码", null);
        if (StringUtils.isNotBlank(passwordMsg)) {
            passwordVal = false;
            msgMap.put("passwordMsg", passwordMsg);
        } else {
            passwordVal = true;
        }
    }

    //二次密码
    private static void confirmPassword(Map<String, String> msgMap, String confirmPassword) {
        String confirmPasswordMsg = valStr(confirmPassword, RegExConstant.PASSWORD, "再次输入密码", null);
        if (StringUtils.isNotBlank(confirmPasswordMsg)) {
            confirmPasswordVal = false;
            msgMap.put("confirmPasswordMsg", confirmPasswordMsg);
        } else {
            confirmPasswordVal = true;
        }
    }

    //2次密码是否相等(2次密码验证通过，2次密码对比)
    private static void passwordEquals(Map<String, String> msgMap, String password, String confirmPassword) {
        if (passwordVal && confirmPasswordVal && !StringUtils.equals(password, confirmPassword)) {
            msgMap.put("confirmPasswordMsg", "两次输入的密码不相同。");
        }
    }


    //验证码
    @Deprecated
    private static void imgCode(Map<String, String> msgMap, String imgCode) {
        String imgCodeMsg = valStr(imgCode, RegExConstant.IMGCODE, "验证码", null);
        if (StringUtils.isNotBlank(imgCodeMsg)) {
            msgMap.put("imgCodeMsg", imgCodeMsg);
        }
    }


}
